package com.link.base.base.product.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.product.dao.mybatis.mapper.ProdChangeMapper;
import com.link.base.base.product.model.ProdChange;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 设计变动
 *
 * @author 阳晓琳
 * @date 2020-10-14 19:28:02
 */
@Service
public class ProdChangeServiceImpl extends BasicServiceImpl<ProdChange> implements ProdChangeService {

    @Resource
    private ProdChangeMapper    prodChangeMapper;

    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<ProdChange> getBasicMapper() {
        return prodChangeMapper;
    }

    /**
     * 设变信息提交
     * @param entity
     * @throws Exception
     */
    @Override
    public void prodChangeSubmit(ProdChange entity) throws Exception {
        ProdChange prodChange = prodChangeMapper.queryById(entity);
        //定义审批流
        String approvalType = "prodChangeSubmit";

        ObjectMapper mapper = new ObjectMapper();
        String approvalDetail = mapper.writeValueAsString(prodChange);
        //创建审批流
        flowService.createFlow(prodChange.getId(), approvalType, null, prodChange.getProjectCode(),prodChange.getId(), approvalDetail);
        //提交审批后，审批状态为【审批中】
        prodChange.setAuditStatus("InApproval");
        prodChangeMapper.update(prodChange);
    }

    /**
     * 审批拒绝与通过方法
     * @param flow
     * @param arg1
     * @throws Exception
     */
    @Override
    public void apprSubmit(Flow flow, String arg1, String arg2, String arg3, String arg4, String arg5) throws Exception {
        Long prodChangeId = flow.getFlowObjId();
        ProdChange prodChange = new ProdChange();
        prodChange.setId(prodChangeId);
        ProdChange prodChange1 = prodChangeMapper.queryById(prodChange);
        prodChange1.setAuditStatus(arg1);
        prodChangeMapper.update(prodChange1);
    }
}